<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!doctype html>
<html>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<jsp:include page="../common/import.jsp" flush="true"></jsp:include>
<body>
<jsp:include page="../common/header.jsp" flush="true"></jsp:include>



<div class="bgfff p20 pb100">
	<div class="container clear h450 pb100">
        <ul class="clear tc f18 mt20 add-store-ul">
        	<li class="fl lihover">搜索门店</li>
            <li class="fl">填写信息</li>
            <li class="fl">预览并提交</li>
        </ul>
        
        <p class="mt30">通过门店搜索进行快速添加</p>
        <input type="hidden" id="qualificationId" value="${qualificationId}">
        <input type="hidden" id="lastId"/>
		<input type="hidden" id="target"/>
		<input type="hidden" id="pageIndex" value="1"/>
		<input type="hidden" id="size"/>
		
        <div class="mt10 max-h">
				<select id="province" name="province" class="b w200 lg40">
					<option>北京市</option>
					<option>天津市</option>
					<option>河北省</option>
					<option>吉林省</option>
					<option>上海市</option>
					<option>山西省</option>
					<option>内蒙古</option>
					<option>辽宁省</option>
					<option>黑龙江省</option>
					<option>江苏省</option>
					<option>浙江省</option>
					<option>安徽省</option>
					<option>福建省</option>
					<option>江西省</option>
					<option>山东省</option>
					<option>河南省</option>
					<option>湖北省</option>
					<option>湖南省</option>
					<option>广东省</option>
					<option>广西</option>
					<option>海南省</option>
					<option>重庆市</option>
					<option>西藏</option>
					<option>宁夏</option>
					<option>四川省</option>
					<option>贵州省</option>
					<option>云南省</option>
					<option>陕西省</option>
					<option>甘肃省</option>
					<option>青海省</option>
					<option>新疆</option>
				</select> 
				<select id="city" name="city" class="b w200 lg40">
                <!-- <option>朝阳区</option> -->
                <option>北京市</option>
                <option>北京市辖县</option>
            </select>
            <input class="w600 p10 h20 ml20" type="text" placeholder="搜索门店名称或mcode" id="shopName"  onkeypress="CheckInfo(event)">
            <button class="p10 bg1eb9ef cfff ml20 b-radius w100 h20 b" onclick="search()">搜索</button>
            
        </div>
         <button id="loadMerchant" type="button" class="btn_2 btn_loading_1" style="display:none;">点击加载更多门店</button>
    </div>
</div>
<jsp:include page="../common/footer.jsp" flush="true"></jsp:include>
<script type="text/javascript">
$(function(){
	var edit_city = '';
	$("#province").change(function () {
		var html = "<option value=''>城市</option>";
		$("#city").html(html);
		
		var prov = "prov";
		var value = $(this).val();
		var url = "http://www.wangpos.com/service/address/refreshCity";
		var data = {"key": prov,"value":value,"url":url};
        $.ajax({
			url: "${base}/agent/agent/authorZone.xhtml",
			type: "POST",
			data: data,
			dataType: "JSON",
			success: function (data) {
				var ison = data +";";
				if (data.status == 0) {
					hintError("数据错误");
					return;
				}
				var list = data.data;
				html = ''
				for (var i = 0; i < list.length; i++) {
					html += '<option value="' + list[i] + '">' + list[i] + '</option>';
				}
				// $("#prov").val(val);
				$("#city").html(html);
				if(edit_city != ''){
					$('#city').val(edit_city);
				}
				$('#city').change();
			},
			error: function (data) {
				alert('服务端报错' + data.status);
			}
		}); 
    });
});

//搜索按钮
 function search() {
    	if($("#shopName").val() == ""){
    		return false;
    	}
    	
    	init();	
        
        var addByHand = "";
        addByHand += "<li id='addByHand'>";
        addByHand += "<div class='card_store' onclick='addShopByHand()'>";
        addByHand += "<a class='card_plus'><img src='${base}/agent/public/images/icon_plus_3.png'></a>";
        addByHand += "<div class='card_store_main3'>";
        addByHand += "<p>手动添加店铺</p>";
        addByHand += "</div>";
        addByHand += "</div>";
    	addByHand += "</li>";
    	
    	$(".card_store_list").html(addByHand);
    	
    	var city = $("#city").val();
    	var shopName = $("#shopName").val();
        var lastId = $("#lastId").val();//""
        var pageIndex = $("#pageIndex").val();//1,size=9,init方法中
        
        city = city.replace(/市/,"");
        
    	doSearch(city, shopName, lastId, pageIndex, 20);
    }
    
    //searchByAdmin
	function doSearch(city, shopName, lastId, pageIndex, limit){
         //searchByPosAdmin
		$.ajax({
			url: "${absUrl}merchant/doSearch",
			type: "POST",
            data: {
           	 "city": city,
           	 "shopName":shopName,
           	 "lastId": lastId,
           	 "pageIndex":pageIndex,
           	 "limit": limit
          	},
            dataType: "json",
            async : false,
            success: function (data) {
				if(data.status == 0){
					var mlist = data.data;
					var mcodeDlag = data.info;
					for(var i=0; i < mlist.length; i++){
						var from = mlist[i].from;
						$("#target").val(from);
						
						if(from == 'soa'){
							$("#lastId").val(mlist[i].storeId);
						}
						
						var html = "<li>";
           			 	html += "<div class='card_store'>";
                        html += "<div class='card_store_k'>";
                        html += "<h4>"+mlist[i].name+"</h4>";    
                        html += "<p>";
                        if(from == 'soa' || mcodeDlag == 'mcode'){
                        	html += "<small>联系电话："+mlist[i].tel+"</small>";
                        }else{
                        	html += "<small>联系电话："+mlist[i].telephone+"</small>";
                        }
                        html += "</p>";
                        html += "<p>";
                        html += "<small>店铺地址：";
                        var address =  mlist[i].address;
                        if (!address==undefined) {
                        	if(from == 'soa' || mcodeDlag == 'mcode'){
	                        	if(mlist[i].address.length > 30){
	                           	 	html += address.address_detail.substring(0, 29)+"...";
	                            }else{
	                           	 	html += address.address_detail;
	                            }
	                        }else{
	                        	if(mlist[i].address.length > 30){
	        						html+=address.substring(0, 29)+"...";
	        					}else{
	        						html+=address;
	        					}
	                        }
						}else{
							html+="";
						}
	                        
                        html += "</small>";
                        html += "</p>";
                        html += "</div>";
                       
                        if(mlist[i].existFlag == 1){
    						html+="<div id='import_"+i+"' class='card_store_main2'>";
    						if(mlist[i].owned == 0){
    							html+="<p>已存在旺POS体系可直接添加</p>";
    							html += "<button class='btn_3' onclick='bindUserMerchant(\""+i+"\",\""+mlist[i].storeId+"\",\""+mlist[i].mcode+"\")'>添加新门店</button>";
    						}else{
    							html+="<p>门店已存在，并被其他商户认领</p>";
    						}
    						html+="</div>";
    					}else{
    						html+="<div class='card_store_main2'>";
    						html+="<p>添加门店时请补全信息</p>";
    						html+="<button class='btn_3' onclick='addByBaidu("+JSON.stringify(mlist[i])+", this)'>添加新门店</button>";
    						html+="</div>";
    					}  
                        html += "</div>";
                        html += "</li>";
                        
						$("#addByHand").before(html);
					}
					if(mlist.length = 8){
						$("#loadMerchant").css("display","block");
					}else{
						$("#loadMerchant").css("display","none");
					}
				}else{
					dangerHint(data.info);
					//location.href= "${absUrl}merchant/index";
				}
			}
		});
	}
//手动添加
function addShopByHand() {
    	if(window.sessionStorage){
    		var s = window.sessionStorage;
			s.removeItem('baidu');
    	}
        window.location = "${absUrl}merchant/addByBaidu";
    }
</script>
</body>
</html>